// -----------------------------------------------------------------------
//  <copyright file="ISettingStore.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024-04-15 21:02</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Domain;


namespace DaprPlus.Settings;

/// <summary>
/// 定义设置信息存储
/// </summary>
public interface ISettingStore
{
    /// <summary>
    /// 获取指定键名的设置项
    /// </summary>
    /// <param name="key">键名</param>
    /// <param name="token"></param>
    /// <returns></returns>
    Task<ISettingItem?> GetByKeyAsync(string key, CancellationToken token = default);

    /// <summary>
    /// 获取指定根节点的设置项，如输入“ServiceName:Users.”，返回所有以此开头的设置项
    /// </summary>
    /// <param name="rootKey">键根节点</param>
    /// <param name="token"></param>
    /// <returns></returns>
    Task<ISettingItem[]> GetByRootKeyAsync(string rootKey, CancellationToken token = default);

    /// <summary>
    /// 创建或更新键值对信息
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="token"></param>
    /// <returns></returns>
    Task<ApiResult> CreateOrUpdateAsync(string key, object value, CancellationToken token = default);

    Task<ApiResult> CreateOrUpdateAsync(ISettingItem[] items, CancellationToken token = default);

    Task<ApiResult> DeleteAsync(string[] keys, CancellationToken token = default);

    Task<ApiResult> DeleteByRootKeyAsync(string rootKey, CancellationToken token = default);

    /// <summary>
    /// 构建指定类型的设置信息
    /// </summary>
    /// <typeparam name="TSetting">设置类型</typeparam>
    /// <returns></returns>
    Task<TSetting> BuildSettingAsync<TSetting>(CancellationToken token = default) where TSetting : ISetting, new();

    /// <summary>
    /// 构建指定类型的设置信息
    /// </summary>
    /// <param name="settingType">设置类型</param>
    /// <param name="setting">设置类型对象</param>
    /// <param name="token"></param>
    /// <returns></returns>
    Task<ISetting> BuildSettingAsync(Type settingType, ISetting? setting = default, CancellationToken token = default);

    /// <summary>
    /// 保存指定类型的设置信息
    /// </summary>
    /// <typeparam name="TSetting"></typeparam>
    /// <param name="setting"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    Task<ApiResult>SaveSettingAsync<TSetting>(TSetting setting, CancellationToken token = default) where TSetting : ISetting;

    /// <summary>
    /// 找出指定类型的设置定义
    /// </summary>
    /// <param name="typeName">类型名称，可是一部分关键字</param>
    /// <param name="includeItems">包含设置项</param>
    /// <param name="token"></param>
    /// <returns></returns>
    Task<SettingDefinition?> GetSettingDefinition(string typeName, bool includeItems = false, CancellationToken token = default);
}
